﻿@inject NavigationManager navManager
@inherits ErrorBoundary

@if ( CurrentException is null )
{
    @ChildContent
}
else if ( ErrorContent is not null )
{
    @ErrorContent(CurrentException)
}
else
{
    <div class="error-panel">
        <p>An error has occurred.</p>
        <button class="btn btn-primary" @onclick="@Reload">Reload Page.</button>
        <button class="btn btn-primary" @onclick="@ToggleDisplay">@ShowText Details.</button>
    </div>
}

@if ( Open )
{
    <div class="damselfly-error-popup">
        <h2>Error Details:</h2>
        <div class="error-stacktrace">
            @foreach ( var exception in receivedExceptions )
            {
                @exception
            }
        </div>
        <div>
            <button class="btn btn-primary" @onclick="@ToggleDisplay">Close</button>
        </div>
    </div>
}

@code {
    List<Exception> receivedExceptions = new();
    bool Open { get; set; } = false;
    string ShowText => Open ? "Hide" : "Show";

    private void Reload()
    {
        navManager.NavigateTo(navManager.Uri, true);
    }

    private void ToggleDisplay()
    {
        Open = !Open;
    }

    protected override Task OnErrorAsync(Exception exception)
    {
        receivedExceptions.Add(exception);
        return base.OnErrorAsync(exception);
    }

    public new void Recover()
    {
        receivedExceptions.Clear();
        base.Recover();
    }

}